Create custom images directly from Telegram with our bot
@ImageGeneratorBot
/start
to beginGenerate an image with your text on a colored background
Create a meme with top and bottom text
Generate an inspirational quote image
Create image with custom background color
<?php
// Set your Telegram bot token
$telegramBotToken = 'YOUR_BOT_TOKEN';
// Get the incoming update from Telegram
$update = json_decode(file_get_contents('php://input'), true);
// Check if this is a valid message
if (isset($update['message'])) {
$chatId = $update['message']['chat']['id'];
$messageText = $update['message']['text'];
// Handle commands
if (strpos($messageText, '/start') === 0) {
sendMessage($chatId, "Welcome to Image Generator Bot! Use /create, /meme, /quote or /color commands to generate images.");
}
elseif (strpos($messageText, '/create') === 0) {
$text = trim(substr($messageText, 7));
if (empty($text)) {
sendMessage($chatId, "Please provide text after /create command");
} else {
$imageUrl = generateTextImage($text);
sendPhoto($chatId, $imageUrl);
}
}
elseif (strpos($messageText, '/meme') === 0) {
$parts = explode('|', trim(substr($messageText, 5)));
if (count($parts) < 2) {
sendMessage($chatId, "Please provide top and bottom text separated by |");
} else {
$imageUrl = generateMemeImage($parts[0], $parts[1]);
sendPhoto($chatId, $imageUrl);
}
}
// Add other command handlers here...
}
// Function to generate text image
function generateTextImage($text) {
// Create a blank image
$image = imagecreatetruecolor(800, 400);
// Allocate colors
$bgColor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200));
$textColor = imagecolorallocate($image, 255, 255, 255);
// Fill background
imagefilledrectangle($image, 0, 0, 800, 400, $bgColor);
// Add text
$font = 'arial.ttf'; // Make sure to have the font file
imagettftext($image, 36, 0, 50, 200, $textColor, $font, $text);
// Save image to temporary file
$filename = tempnam(sys_get_temp_dir(), 'telegram_img') . '.png';
imagepng($image, $filename);
imagedestroy($image);
return $filename;
}
// Function to send message
function sendMessage($chatId, $text) {
global $telegramBotToken;
$url = "https://api.telegram.org/bot$telegramBotToken/sendMessage";
$data = [
'chat_id' => $chatId,
'text' => $text
];
file_get_contents($url . '?' . http_build_query($data));
}
// Function to send photo
function sendPhoto($chatId, $photoPath) {
global $telegramBotToken;
$url = "https://api.telegram.org/bot$telegramBotToken/sendPhoto";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$photo = new CURLFile($photoPath);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
'chat_id' => $chatId,
'photo' => $photo
]);
curl_exec($ch);
curl_close($ch);
// Clean up
unlink($photoPath);
}
?>
https://api.telegram.org/botYOUR_TOKEN/setWebhook?url=https://yourdomain.com/yourscript.php